/**
 * @author Administrator
 */

GameState = { None : 0, TestSwap : 1, MoveBack : 2, MoveDown : 3 };

var CMouse = new Class({
	Extends: CImage,
    initialize: function(url){
        this.parent(url);
    },
	OnDraw : function(){
		if(this.dx != -1){
			this.parent();
		}
	},
	dx : -1,
	dy : 0,
	state : GameState.None
});
myMouse = new CMouse("mouse.png");

OnMouseDown = function(e) {
	var This = myMouse;
	e.page.x -= e.target.offsetLeft;
	e.page.y -= e.target.offsetTop;
	if(e.page.x > 40 * 8 || e.page.y > 42 * 9 ||
		e.page.x < 0 || e.page.y < 0 || This.state != GameState.None){
	 	return;
	}
	
	if(This.dx == -1){
		This.dx = e.page.x;
		This.dy = e.page.y;
		This.x = (This.dx / 40).toInt() * 40 + 1;
		This.y = (This.dy / 42).toInt() * 42 + 1;
	}else{
        var x1 = (This.dx / 40).toInt();
        var y1 = (This.dy / 42).toInt();
        var x2 = (e.page.x / 40).toInt();
        var y2 = (e.page.y / 42).toInt();
		var abs = Math.abs;
        if((x1 == x2 && abs(y1 - y2) == 1) || (y1 == y2 && abs(x1 - x2) == 1)) {
			fruitbox.SwapPoint(x1, y1, x2, y2);
			This.state = GameState.TestSwap;
		} 
			
        This.dy = This.dx = -1;
	}
}

